<template>
  <el-dialog :visible.sync="isShow" width="600" center class="dialog">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" inline>
      <el-form-item label="企业编号" prop="eid">
        <el-input v-model="form.eid"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="企业简称" prop="short_name">
        <el-input v-model="form.short_name"></el-input>
      </el-form-item>
      <el-form-item label="企业简介" prop="intro">
        <el-input v-model="form.intro"></el-input>
      </el-form-item>
      <el-form-item label="企业备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-header" slot="title">
      <span class="title">{{ mode == "add" ? "新增企业" : "编辑企业" }}</span>
      <span class="close el-icon-close" @click="isShow = false"></span>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addcompany, editcompany } from "@/api/company.js";
export default {
  props: ["mode", "companyInfo"],
  watch: {
    isShow() {
      if (!this.isShow) {
        this.form = null;
        this.form = {
          eid: "", //	是	string	企业编号
          name: "", //	是	string	企业名称
          short_name: "", //	是	string	简称
          intro: "", //	是	string	企业简介
          remark: "", //	否	string	备注
        };
        this.$refs.form.resetFields();
      }
      if (this.mode == "edit") {
        this.form = null;
        console.log("编辑", this.companyInfo);
        this.form = this.companyInfo;
      }
    },
  },
  data() {
    return {
      isShow: false,
      form: {
        eid: "", //	是	string	企业编号
        name: "", //	是	string	企业名称
        short_name: "", //	是	string	简称
        intro: "", //	是	string	企业简介
        remark: "", //	否	string	备注
      },
      rules: {
        eid: { requier: true, message: "请输入", trigger: "change" },
        name: { requier: true, message: "请输入", trigger: "change" },
        short_name: { requier: true, message: "请输入", trigger: "change" },
        intro: { requier: true, message: "请输入", trigger: "change" },
        remark: { requier: true, message: "请输入", trigger: "change" },
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.mode == "edit") {
            console.log("bianjiye");
            editcompany(this.companyInfo)
              .then(() => {
                this.$message.success("编辑成功");
                this.isShow = false;
                this.$emit("getData");
              })
              .catch((err) => {
                this.$message.error(err);
              });
          } else if (this.mode == "add") {
            addcompany(this.form)
              .then(() => {
                this.$message.success("新增成功");
                this.isShow = false;
                this.$emit("getData");
              })
              .catch((err) => {
                this.$message.error(err);
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less">
.dialog {
  .dialog-header {
    height: 54px;
    background: linear-gradient(to right, #02c5fa, #1294fa);
    font-size: 18px;
    font-family: Microsoft YaHei Regular, Microsoft YaHei Regular-Regular;
    font-weight: bold;

    color: #ffffff;
    letter-spacing: 0px;
    .title {
      line-height: 54px;
    }
    .close {
      float: right;
      padding: 20px 30px 0 0;
    }
  }
}
</style>
